package com.freak.neteasecloudmusic.modules.find.recommend.songlist.fragment;

import android.graphics.Rect;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.entity.MultiItemEntity;
import com.freak.neteasecloudmusic.R;
import com.freak.neteasecloudmusic.base.BaseAbstractMvpFragment;
import com.freak.neteasecloudmusic.modules.find.recommend.entity.HotSongListEntity;
import com.freak.neteasecloudmusic.modules.find.recommend.entity.SongListEntity;
import com.freak.neteasecloudmusic.modules.find.recommend.songlist.detail.DetailActivity;
import com.freak.neteasecloudmusic.modules.find.recommend.songlist.fragment.adapter.SongListFragmentAdapter;
import com.freak.neteasecloudmusic.modules.find.recommend.songlist.fragment.entity.PlaylistsEntity;
import com.freak.neteasecloudmusic.modules.find.recommend.songlist.fragment.entity.RecommendEntity;
import com.freak.neteasecloudmusic.utils.ToastUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class SongListFragment extends BaseAbstractMvpFragment<SongListFragmentPresenter> implements SongListFragmentContract.View {
    @BindView(R.id.recyclerViewSongList)
    RecyclerView recyclerViewSongList;
    private SongListFragmentAdapter mSongListAdapter;
    private List<MultiItemEntity> mList;
    private Bundle bundle;
    private int type;
    private String name;
    private String cat;
    public int mOffset = 0;
    public final int mLimit = 30;

    @Override
    protected SongListFragmentPresenter createPresenter() {
        return new SongListFragmentPresenter();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_song_list;
    }

    @Override
    protected void initEventAndData() {
        loadData();
    }

    private void loadData() {
        switch (type) {
            //推荐
            case 0:
                mPresenter.getRecommendSongList(100);
                break;
            //官方
            case 1:
                mPresenter.getSearch("精选集");
//                mPresenter.loadHotSongListCategoryList(cat, mLimit, mOffset);
                break;
            //精品
            case 2:
                mPresenter.loadHotSongListCategoryList(cat, mLimit, mOffset);
                break;
            //其他
            default:
                mPresenter.loadSongListCategoryList("new", cat, mLimit, mOffset);
                break;
        }
    }

    @Override
    protected void initView(View view) {
        bundle = getArguments();
        if (bundle == null) {
            mActivity.finish();
            return;
        }
        type = bundle.getInt("type");
        name = bundle.getString("name");
        cat = bundle.getString("cat");
        mList = new ArrayList<>();
        recyclerViewSongList.setLayoutManager(new GridLayoutManager(mActivity, 3));
        mSongListAdapter = new SongListFragmentAdapter(mList, mActivity);
        mSongListAdapter.bindToRecyclerView(recyclerViewSongList);
        mSongListAdapter.setEmptyView(R.layout.view_no_data_common, recyclerViewSongList);
        mSongListAdapter.disableLoadMoreIfNotFullPage();
        recyclerViewSongList.addItemDecoration(new RecyclerView.ItemDecoration() {
            @Override
            public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
                super.getItemOffsets(outRect, view, parent, state);
                outRect.right = 10;
                outRect.left = 10;
            }
        });
        recyclerViewSongList.setAdapter(mSongListAdapter);
        mSongListAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                loadData();
            }
        }, recyclerViewSongList);
        mSongListAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                switch (adapter.getItemViewType(position)) {
                    case SongListFragmentAdapter.SONG_LIST:
                        PlaylistsEntity playlistsEntity= (PlaylistsEntity) adapter.getData().get(position);
                        Bundle bundle=new Bundle();
                        bundle.putString("id",playlistsEntity.getId());
                        gotoActivity(DetailActivity.class,false,bundle);
                        break;
                    case SongListFragmentAdapter.RECOMMEND_SONG_LIST:
                        RecommendEntity.ResultBean resultBean= (RecommendEntity.ResultBean) adapter.getData().get(position);
                        Bundle bundle1=new Bundle();
                        bundle1.putString("id", String.valueOf(resultBean.getId()));
                        gotoActivity(DetailActivity.class,false,bundle1);
                        break;
                }
            }
        });
    }

    @Override
    protected void initLazyData() {

    }

    @Override
    protected void showLoading() {

    }

    @Override
    public void showToast(String toast) {
        if (mOffset>0){
            mSongListAdapter.loadMoreFail();
        }
        ToastUtil.shortShow(toast);
    }

    /**
     * 其他歌单
     *
     * @param songListEntity
     */
    @Override
    public void loadSongListCategoryListSuccess(SongListEntity songListEntity) {
        if (mOffset == 0) {
            mList.clear();
        }
        if (songListEntity != null) {
            if (songListEntity.isMore()) {
                mOffset += mLimit;
            }
            if (mOffset >= songListEntity.getTotal()) {
                mSongListAdapter.loadMoreEnd();
            } else {
                mSongListAdapter.loadMoreComplete();
            }
            mList.addAll(songListEntity.getPlaylists());
        }
        mSongListAdapter.notifyDataSetChanged();
    }

    /**
     * 精品歌单
     *
     * @param hotSongListEntity
     */
    @Override
    public void loadHotSongListCategoryListSuccess(HotSongListEntity hotSongListEntity) {
        if (mOffset == 0) {
            mList.clear();
        }
        if (hotSongListEntity != null) {
            if (hotSongListEntity.isMore()) {
                mOffset += mLimit;
            }
            if (mOffset >= hotSongListEntity.getTotal()) {
                mSongListAdapter.loadMoreEnd();
            } else {
                mSongListAdapter.loadMoreComplete();
            }
            mList.addAll(hotSongListEntity.getPlaylists());
        }
        mSongListAdapter.notifyDataSetChanged();
    }

    /**
     * 推荐歌单
     *
     * @param recommendEntity
     */
    @Override
    public void getRecommendSongListSuccess(RecommendEntity recommendEntity) {
        mList.clear();
        if (recommendEntity != null) {
            mList.addAll(recommendEntity.getResult());
        }
        mSongListAdapter.loadMoreEnd();
        mSongListAdapter.notifyDataSetChanged();
    }
}
